package com.qdlc.p2p.biz.core.tender;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Service;

/**
 * 标策略工厂
 */
@Service
public class BorrowStrategyContextFactory implements BeanPostProcessor {

    private final static Map<Integer, BorrowStrategyContext> borrowStrategyContext
            = new HashMap<Integer, BorrowStrategyContext>();

    public static BorrowStrategyContext getBorrowStrategy(int type) {
        return borrowStrategyContext.get(type);
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof IBorrowStrategy) {
            IBorrowStrategy strategy =  (IBorrowStrategy) bean;
            borrowStrategyContext.put(strategy.getStrategyType(), new BorrowStrategyContext(strategy));
        }
        return bean;
    }
}
